package com;

import com.jfinal.kit.Kv;
import com.jfinal.kit.PathKit;
import com.jfinal.template.Engine;

import java.io.*;
import java.util.Map;

/***
 * jfinal魔板引擎
 * @author zhangyue
 */
public class JfEnjoy {

    /**
     * 根据具体魔板生成文件
     * @param templateFileName  模板文件名称
     * @param kv                渲染参数
     * @param filePath          输出目录
     * @return 
     */
    public boolean render(String templateFileName, Kv kv, StringBuilder filePath)  {
        BufferedWriter output = null;
        try {
            String baseTemplatePath = new StringBuilder(PathKit.getRootClassPath())
            .append("/")
            .append(PathKit.getPackagePath(this))
            .append("/templete")
            .toString();
            System.out.println("baseTemplatePath="+baseTemplatePath);

            File file = new File(filePath.toString());
            
            File path = new File(file.getParent());
            if ( ! path.exists() ) {
                path.mkdirs();
            }
            output = new BufferedWriter(new FileWriter(file));
            
            Engine.use()
            .setBaseTemplatePath(baseTemplatePath)
            //.setSourceFactory(new ClassPathSourceFactory())
            .getTemplate(templateFileName)
            .render(kv, output);
            
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
            try { if( output != null ) output.close(); } catch (IOException e) {}
        }
    }
    
    /**
     */
    public boolean render(String templateFileDir, Kv kv)  {
        File file = new File(templateFileDir);
        if(!file.exists() || file.list().length == 0){
            System.out.println("请添加魔板文件");
            return false;
        }
        for (File classFile :  file.listFiles()){
            if(classFile.getName().contains("control")){
                String aPackage = (String)kv.get("package");
                String outFileName = new StringBuffer(kv.getStr("rootJavaPath")).append("/").append(FileNameUtil.toPathByPackage(aPackage)).append("/control/").append(kv.getStr("className")).append("Control.java").toString();
                generatorCode(templateFileDir,classFile.getName(),kv,outFileName);
            }else if(classFile.getName().contains("service")){
                String aPackage = (String)kv.get("package");
                String outFileName = new StringBuffer(kv.getStr("rootJavaPath")).append("/").append(FileNameUtil.toPathByPackage(aPackage)).append("/service/").append(kv.getStr("className")).append("Service.java").toString();
                generatorCode(templateFileDir,classFile.getName(),kv,outFileName);
            }else{
                String rootHtmlPath = kv.getStr("rootHtmlPath");

                StringBuffer sb = new StringBuffer(rootHtmlPath).append("/");
                if(kv.getStr("smallClassName") != null){
                    sb.append(kv.getStr("smallClassName")).append("_");
                }
                String outFileName = sb.append(classFile.getName()).toString();
                generatorCode(templateFileDir,classFile.getName(),kv,outFileName);
            }
        }
        return false;
    }

    public void generatorCode(String templeteFilePath, String templateFileName, Kv kv, String outFileName){
        File file = new File(outFileName);

        File fileDir = file.getParentFile();
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter(file));
            Engine.use().setBaseTemplatePath(templeteFilePath)
                    .getTemplate(templateFileName)
                    .render(kv,out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(out != null){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
